package com.gohi.main.user.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.commons.validator.Msg;
import org.apache.log4j.Logger;
import org.hibernate.TransactionException;

import com.gohi.core.web.action.BaseAction;
import com.gohi.main.store.hbm.ShopgooAppointmentT;
import com.gohi.main.store.hbm.ShopgooComment;
import com.gohi.main.store.service.ICommentService;
import com.gohi.main.store.service.IStoreService;
import com.gohi.main.system.hbm.ShopgooArea;
import com.gohi.main.system.service.ISystemService;
import com.gohi.main.user.hbm.ShopgooMember;
import com.gohi.main.user.hbm.ShopgooMessage;
import com.gohi.main.user.hbm.ShopgooOrder;
import com.gohi.main.user.hbm.ShopgooWxMember;
import com.gohi.main.user.service.IShopgooWxMemberService;
import com.gohi.main.user.service.IUserService;
import com.gohi.main.util.AppJsonUtils;
import com.gohi.main.util.MD5;
import com.gohi.main.util.MsgUtils;
import com.gohi.main.util.StringUtil;
import com.gohi.main.util.Utils;

import com.gohi.main.wxapi.util.AccoutUtil;
import com.gohi.main.wxapi.vo.JsApi;
import com.gohi.main.wxapi.vo.WxAccount;

/**
 * 用户信息 ACTION
 * 
 * @author Administrator
 * 
 */
public class UserCententAction extends BaseAction {
	Logger logger = Logger.getLogger(UserCententAction.class);
	private ISystemService systemService;
	// 用户
	private IUserService userService;
	private ShopgooMember user;
	// 商户
	private IStoreService storeService;
	// 订单信息
	private ShopgooOrder order;
	// 订单的基本信息
	private ICommentService commentService;
	
	private  ShopgooComment comm;//留言信息
	
	
	//账户退出
	public String exit(){
		String openid = this.getSession().getAttribute("openid").toString();
		this.getSession().removeAttribute("user");
		commentService.reBindWxuser(openid);
		return "exit";
	}
	private IShopgooWxMemberService shopgooWxMemberService;
	public String changeorder(){
		String id =this.getRequest().getParameter("id");
		this.url="centent!orderlist.do";
		if(!Utils.show(id).equals("")){
			int result = commentService.chageOrderstet(id, 4);
			if(result>0){
				this.alert = "取消订单成功，等待审核！";
				return "SUCCESS";
			}else{
				this.alert = "参数错误！";
				return "ERROR";
			}
		}else{
			this.alert = "参数错误！";
			return "ERROR";
		}
	}
	//订单列表
	public String orderlist(){
		String t =this.getRequest().getParameter("t");
		if(t==null){
			
		}
		user = (ShopgooMember) this.getRequest().getSession()
				.getAttribute("user");
		this.getRequest().setAttribute("t", t);
		this.getRequest().setAttribute("list", commentService.getOrdersByUser(user.getMember_id(), t));
		//System.err.println(commentService.getOrdersByUser(user.getMember_id(), t).size());
		return "orderlist";
	}
	//订单详细信息
	public String orderdetail(){
		String id = this.getRequest().getParameter("id");
		String openid = this.getRequest().getParameter("openid");
		//System.err.println(openid);
		user = (ShopgooMember) this.getRequest().getSession().getAttribute("user");
		if(user==null && openid!=null){
			//微信订单里面进来的
			this.getSession().setAttribute("openid", openid);
			user= userService.getById(shopgooWxMemberService.getById(openid).getRealid());
			this.getSession().setAttribute("user", user);
		}
		
		if(id==null){
			return "ERROR";
		}
		order =  commentService.queryOrderById(id);
		//查询订单的支付密码
		this.getRequest().setAttribute("pwd", commentService.queryOrderPwd(id));
		//System.err.println();
		getRequest().setAttribute("flag", getRequest().getParameter("flag"));//标示过来的地方  如果不为空从订单列表过来的
		return "orderdetail";
	}
	
	//留言
	public String comment() {
		String id = this.getRequest().getParameter("id");// 获取订单的ID
		order =  commentService.queryOrderById(id);
		if(order==null){
			this.alert="订单信息错误！";
			this.url="centent!orderlist.do";
			return "ERROR";
		}
		return "comment";
	}
	
	//提交保存评论信息
	public String addcomment(){
		user = (ShopgooMember) this.getRequest().getSession().getAttribute("user");
		this.url="centent!orderlist.do";
		if(comm==null){
			this.alert="评论参数错误！";
			return "ERROR";
		}
		comm.setMember_id(user.getMember_id());
		//手机号码留言XXXX
		if(user.getNickname().trim().length()==11){
			comm.setMember_name(user.getNickname().substring(0, 6)+"*****");
		}else{
			comm.setMember_name(user.getNickname());
		}

		if(commentService.addComment(comm)==1){
			this.alert="评论成功！";
			return "SUCCESS";
		}else{
			this.alert="评论失败！";
			return "ERROR";
		}
	
	}
	
	// 下单action
	public String buyorder() throws ParseException {
		user = (ShopgooMember) this.getRequest().getSession()
				.getAttribute("user");
		if (user == null) {
			this.url="user!index.do";
			this.alert = "您尚未登陆！";
			return "ERROR";
		}
		// ShopgooAppointmentT time;
		String yuyue_time = this.getRequest().getParameter("yuyue_time");// 预约的时间
		//System.err.println(yuyue_time);
		getRequest().setAttribute("yuyue_time", yuyue_time);
		String hour = this.getRequest().getParameter("hour");// 套餐的耗时
		if (order == null) {
			this.alert = "订单信息错误!";
			return "ERROR";
		}

		order.setMember_id(user.getMember_id());
		order.setMember_name(user.getMember_name());
		order.setMobile(user.getMobile());
		String renid = commentService.addOrders(order);
		order.setStart_hour(yuyue_time);
		order.setOrder_id(Integer.parseInt(renid));
		if (!renid.equals("")) {
			
		
			//订单插入成功 进行插入排班信息
			ShopgooAppointmentT a = new ShopgooAppointmentT();
			a.setOrder_id(renid);
			a.setBeautician_id(order.getBeautician_id() + "");
			a.setBeautician_name(order.getBeautician_name());
			a.setStore_id(order.getStore_id());

			a.setAppt_date(StringUtil.getDate(StringUtil.getDate(yuyue_time,
					"yyyy-MM-dd"), "yyyy-MM-dd"));
			//System.err.println(a.getAppt_date());
			
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date dt = Utils.parseFormatted(yuyue_time.trim(),
					"yyyy-MM-dd HH:mm");
			a.setStart_hour(dt.getTime() / 1000); // 开始时间
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(dt);
			c.add(12, Integer.parseInt(hour));
			a.setEnd_hour(c.getTime().getTime()/1000);// 套餐结束时间
			a.setTaocantime(Integer.parseInt(hour)); //套餐的耗时
			a.setTaocan_id(order.getItem_id());//套餐的ID
			
			
			int lin = commentService.addAppointment(a);
			if(lin>0){
				//记录全部创建成功
				//System.err.println("数据OK");
				//如果技师不是为0  表示选了技师  是0表示到店选择的
				//System.err.println(a.getBeautician_id());
				if(!a.getBeautician_id().equals("0")){
					if(commentService.checkOrderTime(renid)==0){
						this.alert = "对不起，该时间段已经被预约!";
						return "ERROR";
					}
				}
				
				return "buyorder";
			}else{
				this.alert = "订单信息错误！";
				return "ERROR";
			}
		}
		this.alert = "插入订单错误!";
		return "ERROR";
	}

	//到店付款开始
	public String pay() {
		String order = this.getRequest().getParameter("id");// 获取订单的ID
		String falg = this.getRequest().getParameter("falg");//标示是否微信过来的
		if(falg==null){
			this.alert="下单成功!";
			commentService.changeOrders(order,"2",this.getSession().getAttribute("openid").toString());//线下支付
		}else if(falg.equals("2")){
			this.alert="微信付款失败！";
			this.url="centent!index.do?id";
			return "ERROR";
		}else{
			this.alert="微信支付成功!";
		}
		//System.err.println(result);

		this.url="centent!orderdetail.do?id="+order;
		
		return "SUCCESS";
	}


	
	// 用户中心
	public String index() {

		return "index";
	}

	// 收藏的店铺
	public String favshop() {
		user = (ShopgooMember) this.getRequest().getSession()
				.getAttribute("user");
		if (user != null) {
			this.getRequest().setAttribute("list",
					storeService.queryFavStore(user.getMember_id() + ""));
			return "favshop";
		}
		return "ERROR";
	}

	// 个人资料
	public String userinfo() {
		user = (ShopgooMember) this.getRequest().getSession()
				.getAttribute("user");
		if (user != null) {
			user = userService.getById(user.getMember_id() + "");
			return "userinfo";
		}
		return "ERROR";
	}

	// 更新一个字段
	public void set() throws IOException {
		String key = this.getRequest().getParameter("key");
		String value = this.getRequest().getParameter("value");
		getResponse().setContentType("application/json;charset=GBK");
		user = (ShopgooMember) this.getRequest().getSession()
				.getAttribute("user");
		if (user != null) {
			// 更新
			userService.updateKey(key, value, user.getMember_id() + "");
			this.getResponse().getWriter().print("{\"status\":\"success!\"}");
		} else {
			this.getResponse().getWriter().print("{\"status\":\"error!\"}");

		}

	}

	// 修改密码
	public void repass() throws IOException {
		String password = this.getRequest().getParameter("password");// 新密码
		String fm_pwd = this.getRequest().getParameter("fm_pwd0");// 原始密码
		getResponse().setContentType("application/json;charset=GBK");
		user = (ShopgooMember) this.getRequest().getSession()
				.getAttribute("user");
		if (user != null) {
			user = userService.getById(user.getMember_id() + "");
			// 更新
			if (user.getPassword().equals(MD5.MD5Encode(fm_pwd))) {
				// 密码验证成功
				userService.updateKey("password", MD5.MD5Encode(password),
						user.getMember_id() + "");
				// 修改成功
				this.getResponse().getWriter()
						.print("{\"status\":\"success!\"}");
			} else {
				this.getResponse().getWriter().print("{\"status\":\"error!\"}");
			}
		} else {
			this.getResponse().getWriter().print("{\"status\":\"error!\"}");

		}

	}

	// 密码修改
	public String password() {

		return "password";
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public ShopgooMember getUser() {
		return user;
	}

	public void setUser(ShopgooMember user) {
		this.user = user;
	}

	public IStoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(IStoreService storeService) {
		this.storeService = storeService;
	}

	public ShopgooOrder getOrder() {
		return order;
	}

	public void setOrder(ShopgooOrder order) {
		this.order = order;
	}

	public ICommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(ICommentService commentService) {
		this.commentService = commentService;
	}
	public ShopgooComment getComm() {
		return comm;
	}
	public void setComm(ShopgooComment comm) {
		this.comm = comm;
	}
	public IShopgooWxMemberService getShopgooWxMemberService() {
		return shopgooWxMemberService;
	}
	public void setShopgooWxMemberService(
			IShopgooWxMemberService shopgooWxMemberService) {
		this.shopgooWxMemberService = shopgooWxMemberService;
	}
	
}
